package com.yanchangapp.adapter.part;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zxl.baselib.util.time.TimeUtil;
import com.yanchangapp.R;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.MsgContentBean;
import com.yanchangapp.util.CommonHelper;
import com.zxl.baselib.util.window.DisplayHelper;

import java.util.List;

/**
 * 消息内容的容器...
 */
public class MsgContentAdapter extends RecyclerView.Adapter<MsgContentAdapter.ViewHolder> {
    private Context mContext;
    private List<MsgContentBean> mList;
    private boolean isEdit = false;
    private MsgCntListener mMsgCntListener;
    public MsgContentAdapter(Context context, List<MsgContentBean> list,MsgCntListener msgCntListener) {
        mContext = context;
        mList = list;
        this.mMsgCntListener = msgCntListener;
    }

    public void setEditState(boolean isEdit){
        this.isEdit = isEdit;
    }

    @Override
    public MsgContentAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_msg_content, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MsgContentAdapter.ViewHolder holder, int position) {
        // 消息内容
        MsgContentBean msgContentBean = mList.get(position);
        // 设置头像

        if (TextUtils.isEmpty(msgContentBean.getName())){
            holder.tvName.setTextSize(12);
        }else {
            holder.tvName.setTextSize(15);
        }
        holder.tvName.setText(CommonHelper.getSingleName(msgContentBean.getName()));

        boolean hadRead = msgContentBean.getMsgReadStatus() != AppConstant.MSG_UN_READ;

        if (this.isEdit){
            holder.rlEditStyle.setVisibility(View.VISIBLE);
            holder.rlLookStyle.setVisibility(View.GONE);
        }else {
            holder.rlEditStyle.setVisibility(View.GONE);
            holder.rlLookStyle.setVisibility(View.VISIBLE);
        }
        // 设置已读未读的
        holder.rlLookStyle.setSelected(hadRead);
        holder.tvMsgCnt.setSelected(hadRead);
        holder.tvMsgCnt.setText(msgContentBean.getMsgContent());
        holder.tvMsgData.setText(TimeUtil.getTimeDes(msgContentBean.getCreateTime()));
        // 设置默认为未选中
        holder.checkBox.setChecked(false);
        holder.checkBox.setOnCheckedChangeListener(new MyCheckChangeListener(holder.getAdapterPosition()));
        holder.itemView.setOnClickListener(new ItemClickListener(holder.getAdapterPosition(),msgContentBean,holder.checkBox));
    }

    public class MyCheckChangeListener implements CompoundButton.OnCheckedChangeListener {
        private int position;

        public MyCheckChangeListener(int position) {
            this.position = position;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mMsgCntListener != null){
                mMsgCntListener.onCheckChangedListener(position,isChecked);
            }
        }
    }

    public class ItemClickListener implements View.OnClickListener{
        private int position;
        private MsgContentBean mMsgContentBean;
        private CheckBox mCheckBox;

        public ItemClickListener(int position, MsgContentBean msgContentBean,CheckBox checkBox) {
            this.position = position;
            mMsgContentBean = msgContentBean;
            this.mCheckBox = checkBox;
        }

        @Override
        public void onClick(View v) {
            if (mMsgCntListener != null){
                if (mCheckBox != null && isEdit){
                    mCheckBox.toggle();
                }
                mMsgCntListener.onItemClickListener(position,mMsgContentBean);
            }
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvMsgCnt,tvMsgData,tvName;
        // 编辑状态 查看状态
        private View rlEditStyle,rlLookStyle;
        private CheckBox checkBox;
        public ViewHolder(View itemView) {
            super(itemView);
            rlEditStyle = itemView.findViewById(R.id.rlEditStyle);
            rlLookStyle = itemView.findViewById(R.id.rlLookStyle);
            tvMsgCnt = itemView.findViewById(R.id.tvMsgContent);
            tvMsgData = itemView.findViewById(R.id.tvMsgTime);
            checkBox = itemView.findViewById(R.id.checkbox);
            tvName = itemView.findViewById(R.id.tvTextSingleName);
        }
    }

    public interface MsgCntListener{
        void onItemClickListener(int position,MsgContentBean msgContentBean);
        void onCheckChangedListener(int position,boolean state);
    }
}
